package com.usian.admin.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 通过代码的方式 生成交换机 生成队列 并且 绑定交换机和队列
 */
@Configuration
public class RabbitMQConfig {



    public static final String ADMINEXCHANGE = "adminExchange";
    public static final String ADMINQUEUE = "adminQueue";

    @Bean(ADMINEXCHANGE)
    public Exchange AUTHEXCHANGE(){
        // 交换机是否持久化
        return ExchangeBuilder.directExchange(ADMINEXCHANGE).delayed().durable(true).build();
    }
    //声明队列
    @Bean(ADMINQUEUE)
    public Queue AUTHQUEUE(){
        // 队列默认持久化
        return new Queue(ADMINQUEUE);
    }


    //绑定交换机和队列
    @Bean
    public Binding AUTHEXCHANGE_AUTHQUEUE(@Qualifier(ADMINEXCHANGE) Exchange exchange,@Qualifier(ADMINQUEUE) Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("admin").noargs();
    }


}
